package com.wwc.bwcj.adapter

import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.wwc.bwcj.R
import com.wwc.bwcj.entity.OrderItem
import com.wwc.bwcj.activity.MainActivity

/**
 * 用于订单历史页面的订单适配器,负责展示用户的订单列表
 */
class OrderAdapter(private var items: MutableList<OrderItem>) : RecyclerView.Adapter<OrderAdapter.ViewHolder>() {

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val imgProduct: ImageView = itemView.findViewById(R.id.img_product)
        val tvTitle: TextView = itemView.findViewById(R.id.tv_title)
        val tvTime: TextView = itemView.findViewById(R.id.tv_time)
        val tvTotal: TextView = itemView.findViewById(R.id.tv_total)
        val btnReorder: Button = itemView.findViewById(R.id.btn_reorder)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_order, parent, false)
        return ViewHolder(view)
    }

    // 绑定数据
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        // 获取当前项
        val item = items[position]

        // 加载商品图片
        holder.imgProduct.setImageResource(item.productImage)

        holder.tvTitle.text = item.title
        holder.tvTime.text = "下单时间：${item.orderTime}"
        holder.tvTotal.text = "总价：¥${item.totalPrice}"

        holder.btnReorder.setOnClickListener {
            // 跳转到点单页面
            val intent = Intent(holder.itemView.context, MainActivity::class.java)
            holder.itemView.context.startActivity(intent)
        }
    }

    // 获取数据项数
    override fun getItemCount(): Int = items.size
}
